import Vue from 'vue'
import Vuex from 'vuex'

import createPersistedstate from 'vuex-persistedstate'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		token:"",		// token信息
		userInfo:{},	// 管理员信息
		steam:"",		// 管理员权限
		urlParma:{},	// 页面传递的参数
		talentLiNum:1,	//人才审核状态筛选
		zhaopinLiNum:1,	//招聘审核状态筛选
		qiuzhiLiNum:1,	//求职审核状态筛选
	},
	mutations: {
		// 设置权限
		setToken(state,val){
			state.token = val.token || "";
			state.steam = val.gteam || "";
		},

		setUserInfo(state,val){
			state.userInfo = val || {};
		},

		setUrlParma(state,val){
			for(let key in val){
				state.urlParma[key] = val[key];
			}
		},
		setTalentLiNum(state,val){
			state.talentLiNum = val.talentLiNum || 1;
		},
		setZhaopinLiNum(state,val){
			state.zhaopinLiNum = val.zhaopinLiNum || 1;
		},
		setQiuzhiLiNum(state,val){
			state.qiuzhiLiNum = val.qiuzhiLiNum || 1;
		},
		clearParma(state,val){
			state.urlParma = {}
		},
		outLogin(state,val){
			state.urlParma = {}
			state.userInfo = {};
			state.token = ""
			state.steam = ""
		}
	},
	getters: {
		isRole(state){
			return function(role){
				let isTrue = false;
				if(state.steam=="*"){
					isTrue = true;
					return isTrue;
				}
				if(typeof role=="string"){
					if(state.steam.indexOf(role)!=-1){
						isTrue = true;
					}
				}else{
					let isObjTrue = false;
					for(let i=0;i<role.length;i++){
						if(state.steam.includes(role[i])){
							isObjTrue = true;
						}
					}
					isTrue = isObjTrue
				}
				
				return isTrue;
			}
		},
		
	},
	plugins: [
		createPersistedstate({
			storage: window.sessionStorage,
			key: 'store', // 存数据的key名   自定义的  要有语义化
			// paths: ['loginModule'] // 要把那些模块加入缓存
			render(state) {
				// 要存储的数据：本项目采用es6扩展运算符的方式存储了state中所有的数据
				return {
					...state
				};
			}
		})
	]
})

export default store
